//清除页面里所有INPUT的 单引号 双引号 和
function addEvent() {
	var ipall = document.all.tags("INPUT");
	for(var dp=0;dp<ipall.length;dp++){
		ipall[dp].attachEvent("onkeyup", ckeckInputAll);
	}
	var teall = document.all.tags("TEXTAREA");
	for(var dt=0;dt<teall.length;dt++){
		teall[dt].attachEvent("onkeyup", ckeckInputAll);
	}
}
function ckeckInputAll() {
	var dom = event.srcElement;
	if(dom.tagName=='INPUT'){
		if(dom.type=='text'){
			var text = dom.value.replace(/['"]/g,'');
			if(dom.value.length > text.length)
				dom.value = text;
		}
	}else if(dom.tagName=='TEXTAREA') {
		var text = dom.value.replace(/['"]/g,'');
		if(dom.value.length > text.length)
		dom.value = text;
	}
}
window.attachEvent("onload", addEvent);
//结束
/**
   * 公共弹层alert方法--韩飞
*/
function _alert($msg, $time) {
	try{
		if (typeof(top.MessageMgr)!="undefined") {
		    $time = $time || 3;
		    top.__popup($msg, $time, window);
		}else {
			alert($msg);
		}
	}catch(e){
		alert($msg);
	}
}

//获取指定域内需要的控件的值生成键值对
function cellvalue($dom,names) {
  var ips = $dom.all.tags("INPUT");
  var txts = $dom.all.tags("TEXTAREA");
  var sls = $dom.all.tags("SELECT");
  var content={};
  for (var i=0; i<ips.length; i++) {
  	if(ips[i].name!=null&&ips[i].name!=""){
	  	if(names.indexOf(ips[i].name)==-1){
	  		continue;
	  	}else{
	  		if(checkd(ips[i])){
	  			content[ips[i].name]=ips[i].value;
	  		}else{
	  			return false;
	  		}
	  	}
	}
  }
  for (var i=0; i<txts.length; i++) {
  	if(txts[i].name!=null&&txts[i].name!=""){
  	if(names.indexOf(txts[i].name)==-1){
  		continue;
  	}else{
  		if(checkd(txts[i])){
  			content[txts[i].name]=txts[i].value;
  		}else{
  			return false;
  		}
  	}
  	}
  }
  for (var i=0; i<sls.length; i++) {
  	if(sls[i].name!=null&&sls[i].name!=""){
  	if(names.indexOf(sls[i].name)==-1){
  		continue;
  	}else{
  		if(checkd(sls[i])){
  			content[sls[i].name]=sls[i].value;
  		}else{
  			return false;
  		}
  	}
  	}
  }
  
  return content;
}
//页面排序方法如有变动请在页面上重写此方法
function doOrder(order) {
	if(document.all["order"]){
		if(document.all["taxis"].value==""||document.all["taxis"].value=='asc'){
			document.all["taxis"].value="desc";
		}else{
			document.all["taxis"].value="asc";
		}
		document.all["order"].value=order;
		document.forms[0].submit();
	}
}
function checkForm(){
	var isValid = false;
    var oRequired = new required();   
    for (x in oRequired) {
	    var olength = oRequired[x].length-2;
	    
	    for(i=0;i<olength;i++){
	      
		    valiType =  oRequired[x][2+i] ;

		    switch(valiType) {
		        //不能为空  checkBlank checkSelect checkSelects
		       case "checkBlank": isValid =  checkBlank(oRequired[x]) ;break;
		       case "checkSelect":isValid =  checkSelect(oRequired[x]);break;
		       case "checkSelects":isValid =  checkSelects(oRequired[x]);break;		       
		       //数字类型  checkNaN
		       case "checkNaN": isValid =  checkNaN(oRequired[x]);break;		       
		       //正整数    checkInt
		       case "checkInt": isValid =  checkInt(oRequired[x]);break;		       
		       //日期类型  checkDate
		       case "checkDate":isValid =  checkDate(oRequired[x]);break;
		    }
		    if(isValid==false)  return isValid;
	     }
    }
	return isValid;
}
function doChecks(){
	var isV = false;
	var inputs = document.getElementsByTagName("INPUT");
		for(i=0;i<inputs.length;i++){
		//过滤掉不需要验证的内容
		   	if((!inputs[i].check ) || (inputs[i].disabled) ) continue;
		    
		   	var checkType = inputs[i].check.split(",");
			    for(l=0;l<checkType.length;l++){
				    valiType =  checkType[l] ;
				    switch(valiType) {
				       case "blank":	isV = blankCheck(inputs[i]) ;break;	       
				       case "NaN":		isV = NaNCheck(inputs[i]) ;break;	 	       
				       case "int":		isV = intCheck(inputs[i]) ;break;	 	       
				       case "date":		isV = dateCheck(inputs[i]) ;break;	 
				    }
				    if(isV)return false;
			    }
		}
		var selects = document.getElementsByTagName("SELECT");
		for(i=0;i<selects.length;i++){ 
		
		   	if((!selects[i].check ) || (selects[i].disabled ) ) continue;
		   	
		   	if(selects[i].read&&selects[i].read=="songli"){continue;} 
		   	
		   	var checkType = selects[i].check.split(",");
			    for(l=0;l<checkType.length;l++){
				    valiType =  checkType[l] ;
				    switch(valiType) {
				       case "blank":isV = blankCheck(selects[i]) ;break;	       
				    }
				    if(isV)return false;
			    }
		}
		return true;
}
// input 是否为空
function checkBlank(obj){
	var field = document.forms[0][obj[0]];
	
	try{	
		if(field.length){
			for(s=0;s<field.length;s++){
				if(trim(field[s].value) == "") {
					if( field[s] .disabled)  return true;
					_alert(obj[1]+"不能为空");
					field[s].focus();
					return false;
				}
			}
		}else{
			if(trim(field.value) == "") {
				if( field .disabled)  return true;
				_alert(obj[1]+"不能为空")	;
				try{field.focus();}catch(e){}
				return false;
			}
		}
	}catch(e){
		return false;
	}
	return true;
}

//普通select 是否为空
function checkSelect(obj){
	   var field = document.forms[0][obj[0]];
	  if(field.value==""||field.value=="请选择"){
		  if( field .disabled)  return true;
	  	_alert(obj[1]+"不能为空")
		try{field.focus();}catch(e){}
		return false;
	  }
	  return true;
}

//数组select 是否为空
function checkSelects(obj){
	var field = document.forms[0][obj[0]];
    var selectHidden  =document.forms[0]["selectHidden"];
	if(selectHidden.length){
		for(s=0;s<selectHidden.length;s++){
			if(field[s].value==""||field[s].value=="请选择"){
				if( field[s] .disabled)  return true;
		  		_alert(obj[1]+"不能为空")
				field[s].focus();
				return false;			
			}
		}
	}else{
		if(field.value==""||field.value=="请选择"){
			if( field .disabled)  return true;
		  		_alert(obj[1]+"不能为空")
				try{field.focus();}catch(e){}
				return false;			
			}
	}
	return true;
}

//数字类型 
function checkNaN(obj){ 
	var field  = document.forms[0][obj[0]];
	if(field.length){
		for(s=0;s<field.length;s++){
			if(trim(field[s].value)!=""&&isNaN(trim(field[s].value))){
			if( field[s] .disabled)  return true;
			_alert(obj[1]+"应该为数字类型");
			field[s].focus();
			s = field.length;
			return false;
			}
		}
	}else{
		if(trim(field.value)!=""&&isNaN(trim(field.value))){
			if( field .disabled)  return true;
			_alert(obj[1]+"应该为数字类型")
			try{field.focus();}catch(e){}
			return false;
			}
	}
	return true;
}
function checkInt(obj){ 
	var field  = document.forms[0][obj[0]];
	if(field.length){
		for(s=0;s<field.length;s++){
			var V = trim(field[s].value);
			if(V!=""&& (V.indexOf("-")!=-1 || vInt(V) ) ){
			if( field[s] .disabled)  return true;
			_alert(obj[1]+"应该为正整数类型")
			field[s].focus();
			s = field.length;
			return false;
			}
		}
	}else{
		V = trim(field.value);
		if(V!=""&& (V.indexOf("-")!=-1 || vInt(V) ) ){
			if( field .disabled)  return true;
			_alert(obj[1]+"应该为正整数类型")
			try{field.focus();}catch(e){}
			return false;
			}
	}
	return true;
}

function checkDate(obj){
	var field  = document.forms[0][obj[0]];
	if(field.length){
		for(s=0;s<field.length;s++){
			var V = trim(field[s].value);
			if(V!=""&&vDate(V)){
				if( field[s].disabled)  return true;
				_alert(obj[1]+"应该为规定日期类型 yyyy/mm/dd")
				field[s].focus();
				s = field.length;
				return false;
			}
		}
	}else{
		V = trim(field.value);
		if(V!=""&&vDate(V)){
			if( field.disabled)  return true;
			_alert(obj[1]+"应该为规定日期类型 yyyy/mm/dd")
			try{field.focus();}catch(e){}
			return false;
		}
	}
	return true;
}
function trim(s) {
  return s.replace( /^"s*/, "" ).replace( /"s*$/, "" );
}

function vDate(value) {
	if(trim(value).length!=0){
		var date = value.split("/");
		var yyyy = date[0], mm = date[1], dd = date[2];
		var d=new Date(yyyy,mm-1,dd),
		year=d.getYear()+"",
		mon=d.getMonth()+1,
		day=d.getDate();
		if(year.length<4){
			year="19"+year;
		}
		if ( year!=yyyy || mon!=mm || day!=dd ||!isValidDate(dd, mm, yyyy) )
		    return true;
	}
}
function isValidDate(day, month, year) {
        if (month < 1 || month > 12) {
            return false;
        }
        if (day < 1 || day > 31) {
            return false;
        }
        if ((month == 4 || month == 6 || month == 9 || month == 11) &&
            (day == 31)) {
            return false;
        }
        if (month == 2) {
            var leap = (year % 4 == 0 &&
               (year % 100 != 0 || year % 400 == 0));
            if (day>29 || (day == 29 && !leap)) {
                return false;
            }
        }
        return true;
}

function vInt(value) {
    if(value.search(/^[-\+]?\d+$/) == -1){
    	return true;
    }
}

function doValidete(obj,org){

	var orgs = org.split(",");
	var v = obj.parentNode.parentNode.childNodes[orgs[orgs.length-1]].firstChild;
	var vM = "0";	
	if(orgs.length){		
		for(i = 0;i<orgs.length-1;i++){			
			var og = obj.parentNode.parentNode.childNodes[orgs[i]].firstChild;	
			var vT = og.value;
			if(vT == ""){
				vT = "0";
			}
			vM = parseFloat(vM) + parseFloat(vT);		
		}	

	}
	var vV = obj.value;
	
	if(vV == ""){
		vV = "0";
	}
	
	v.value = parseFloat(vV) * parseFloat(vM);	
	//四舍五入，取小数点的后2位
	v.value =Math.round(v.value*100)/100.00;
}

function doFocus(obj){
	if(obj.value == "0"){
		obj.value ="";
	}
}
//为单独一个控件进行验证
function checkd(obj){
	var isV = false;
	var inputs = obj;
   	if((!inputs.check ) || (inputs.disabled)) return true;
   	var checkType = inputs.check.split(",");
	    for(l=0;l<checkType.length;l++){
		    valiType =  checkType[l] ;
		    switch(valiType) {
		       case "checkBlank":isV = blankCheck(inputs) ;break;	       
		       case "checkNaN": isV = NaNCheck(inputs) ;break;	 	       
		       case "checkInt":isV = intCheck(inputs) ;break;	 	       
		       case "checkDate":isV = dateCheck(inputs) ;break;	 
		       case "checkChar":isV = charCheck(inputs) ;break;
		       case "checkEmail":isV = isEmail(inputs) ;break;
		    }
		    if(isV)return false;
	    }
	return true;
}

//为全局控件进行验证
function check(){
	var isV = false;
//	var oRequired = new required();   
	var selects = document.getElementsByTagName("SELECT");
		for(i=0;i<selects.length;i++){
		   	if((!selects[i].check ) || (selects[i].disabled)) continue;
		   	var checkType = selects[i].check.split(",");
			    for(l=0;l<checkType.length;l++){
				    valiType =  checkType[l] ;
				    switch(valiType) {
				       case "checkBlank":isV = blankCheck(selects[i]) ;break;	       
				    }
				    if(isV)return false;
			    }
		}
	var inputs = document.getElementsByTagName("INPUT");
		for(i=0;i<inputs.length;i++){
		   	if((!inputs[i].check ) || (inputs[i].disabled)) continue;
		   	var checkType = inputs[i].check.split(",");
		    
			    for(l=0;l<checkType.length;l++){
				    valiType =  checkType[l] ;
				    switch(valiType) {
				       case "checkBlank":isV = blankCheck(inputs[i]) ;break;	       
				       case "checkNaN": isV = NaNCheck(inputs[i]) ;break;	 	       
				       case "checkInt":isV = intCheck(inputs[i]) ;break;	 	       
				       case "checkDate":isV = dateCheck(inputs[i]) ;break;	 
				       case "checkChar":isV = charCheck(inputs[i]) ;break;
				       case "checkEmail":isV = isEmail(inputs[i]) ;break;
				    }
				    if(isV)return false;
			    }
		}
	var textareas = document.getElementsByTagName("TEXTAREA");
		for(i=0;i<textareas.length;i++){
		   	if((!textareas[i].check ) || (textareas[i].disabled)) continue;
		   	var checkType = textareas[i].check.split(",");
			    for(l=0;l<checkType.length;l++){
				    valiType =  checkType[l] ;
				    switch(valiType) {
				       case "checkBlank":isV = blankCheck(textareas[i]) ;break;	       
				    }
				    if(isV)return false;
			    }
		}
		return true;
}

function blankCheck(field){
			var str = field.value;
			str = str.replace(/^\s+\s+$/g,"");
			if( str  == "") {
				_alert(field.title+"不能为空")	;
				try{field.focus();}catch(e){}
				return true;
			}
}
 
function NaNCheck(field){
		if(trim(field.value)!=""&&isNaN(field.value)){
			_alert(field.title+"应该为数字类型")
			try{field.focus();}catch(e){}
			return true;
			}
}
function intCheck(field){ 
		if(trim(field.value)!=""&& (field.value.indexOf("-")!=-1 || vInt(field.value) ) ){
			_alert(field.title+"应该为正整数类型")
			try{field.focus();}catch(e){}
			return true;
			}
}
function dateCheck(field){
		if(field.value!=""&&vDate(field.value)){
			_alert(field.title+"应该为规定日期类型 yyyy/mm/dd")
			try{field.focus();}catch(e){}
			return true;
		}
}

function charCheck(field){
	 var reg = "^[A-Za-z]+$";   
        var reg_obj = new RegExp(reg);
		if(field.value!=""&&!field.value.match(reg_obj)){
			_alert(field.title+"应该为字母类型")
			try{field.focus();}catch(e){}
			return true;
		}
}
/**检查邮件地址合法性*/
function isEmail(field) {
	if(field.value!=""){
	    if (field.value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/) == -1) {
	    	_alert(field.title+"格式不正确");
	    	try{field.focus();}catch(e){}
	        return true;
	    }
    }
}
